    program add_exch_codes
    character*6 ticker
    character*8 cusp
    integer(kind=8) nshrs,nvalue,jvalue,ivol,numshrs
    dimension ibg(1000000,4),iend(1000000,4),icode(1000000,4),nc(1000000)
!      open(unit=10,file='e:\FIS_duration\exchange_codes.prn')
!      open(unit=11,file='e:\FIS\crsp_util_sorted_fees_2005_2023.txt')
!      open(unit=12,file='e:\FIS_duration\episode_data.txt')
!      open(unit=98,file='e:\FIS\crsp_util_sorted_fees_2005_2023_exc.txt')
!      open(unit=99,file='e:\FIS_duration\episode_data_exc.txt')
      open(unit=10,file='e:\FIS_duration\exchange_codes.prn')
      open(unit=11,file='e:\FIS\crsp_util_sorted_fees_2005_2023_rep.txt')
      open(unit=12,file='e:\FIS_duration\episode_data_rep.txt')
      open(unit=98,file='e:\FIS\crsp_util_sorted_fees_2005_2023_exc_rep.txt')
      open(unit=99,file='e:\FIS_duration\episode_data_exc_rep.txt')
      nc=0
      ibg=0
      ipl=0
      icdl=0
1     read(10,1000,end=10)ip,idt,icd
      if(icd.eq.0) go to 1
!      write(6,1000)ip,idt,icd
1000  format(1x,i5,1x,i8,1x,i1)      
      if(ip.ne.ipl.or.icd.ne.icdl) nc(ip)=nc(ip)+1
      if(ibg(ip,nc(ip)).eq.0) ibg(ip,nc(ip))=idt
      iend(ip,nc(ip))=idt
      icode(ip,nc(ip))=icd
      ipl=ip
      icdl=icd
      go to 1
10    continue      
20    read(11,1100,end=30)jast,jdate,cusp,ticker,itickt,numshrs,jvalue,age,favg,fmax,fmin,fstd,iperm,price,ivol,retrn,ishrout,vwret,util,nshrs,nvalue      
1100  format(2(1x,i8),1x,a8,2x,a6,4x,i9,2(1x,i14),5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f9.6,1x,i9,1x,f9.6,1x,f6.2,2(1x,i13)) 
      jcode=0
      if(jdate.ge.ibg(iperm,1).and.jdate.le.iend(iperm,1)) jcode=icode(iperm,1)
      if(jdate.ge.ibg(iperm,2).and.jdate.le.iend(iperm,2)) jcode=icode(iperm,2)
      if(jdate.ge.ibg(iperm,3).and.jdate.le.iend(iperm,3)) jcode=icode(iperm,3)
      if(jdate.ge.ibg(iperm,4).and.jdate.le.iend(iperm,4)) jcode=icode(iperm,4)
      write(98,9800)jast,jdate,cusp,ticker,itickt,numshrs,jvalue,age,favg,fmax,fmin,fstd,iperm,price,ivol,retrn,ishrout,vwret,util,nshrs,nvalue,jcode      
!      write(6,9800)jast,jdate,cusp,ticker,itickt,numshrs,jvalue,age,favg,fmax,fmin,fstd,iperm,price,ivol,retrn,ishrout,vwret,util,nshrs,nvalue,jcode      
9800  format(2(1x,i8),1x,a8,2x,a6,4x,i9,2(1x,i14),5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f9.6,1x,i9,1x,f9.6,1x,f6.2,2(1x,i13),1x,i1) 
      go to 20
30    continue     
40    read(12,1200,end=900)nep,jast,jdate,cusp,ticker,itickt,numshrs,age,favg,fmax,fmin,fstd,iperm,price,ivol,retrn,ishrout,vwret,util,nshrs     
1200  format(1x,i4,2(1x,i8),1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13) 
      jcode=0
      if(jdate.ge.ibg(iperm,1).and.jdate.le.iend(iperm,1)) jcode=icode(iperm,1)
      if(jdate.ge.ibg(iperm,2).and.jdate.le.iend(iperm,2)) jcode=icode(iperm,2)
      if(jdate.ge.ibg(iperm,3).and.jdate.le.iend(iperm,3)) jcode=icode(iperm,3)
      if(jdate.ge.ibg(iperm,4).and.jdate.le.iend(iperm,4)) jcode=icode(iperm,4)
      write(99,9900)nep,jast,jdate,cusp,ticker,itickt,numshrs,age,favg,fmax,fmin,fstd,iperm,price,ivol,retrn,ishrout,vwret,util,nshrs,jcode     
9900  format(1x,i4,2(1x,i8),1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i1)
      go to 40

900   iend=iend+1
      stop

    end program add_exch_codes
